package com.shxy.controller;

import com.alibaba.fastjson.JSONObject;
import com.shxy.common.ApiResult;
import com.shxy.common.ResultCode;
import com.shxy.entity.Staff;
import com.shxy.service.IStaffService;
import com.shxy.util.SimpleDate;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工管理接口
 */
@RestController
@RequestMapping("/v2/staff")
@Log
public class StaffController {
    @Autowired
    private IStaffService staffService;

    /**
     * 查询员工信息列表
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ApiResult list(@RequestParam(name = "name", defaultValue = "", required = false) String name,
                          @RequestParam(name = "sex", defaultValue = "", required = false) String sex,
                          @RequestParam(name = "page", defaultValue = "1", required = false) int page,
                          @RequestParam(name = "size", defaultValue = "10", required = false) int size) {

        // 1、分页查询员工信息
        Page<Staff> pagedList = staffService.list(name, sex, page, size);

        // 2、如果查询到内容
        if(pagedList.hasContent()) {
            pagedList.getContent().stream().forEach(staff -> {
                staff.setAge(SimpleDate.calculateAge(staff.getBirthday()));
            });
        }

        JSONObject obj = new JSONObject();
        obj.put("total", pagedList.getTotalElements());
        obj.put("items", pagedList.getContent());
        return ApiResult.success(obj);
    }


    /**
     * 保存员工信息
     * @param staff
     * @return
     */
    @PostMapping("/save")
    public ApiResult add(@RequestBody Staff staff) {
        // 1、如果身份证号码存在，则返回错误
        if(staffService.exists(staff)) {
            return ApiResult.fail(ResultCode.PARAM_NOT_VALID, "身份证号码已存在");
        }
        // 2、保存用户信息
        staffService.addNew(staff);

        return ApiResult.success();
    }

    /**
     * 根据编号删除员工信息
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ApiResult delete(@PathVariable(name = "id") int id) {
        // 1、如果该员工存在
        if(staffService.exists(id)) {
            staffService.deleteById(id);
            return ApiResult.success();
        }
        else {
            return ApiResult.fail(ResultCode.PARAM_NOT_VALID, "该员工不存在!");
        }
    }

    /**
     * 列表格式转换
     * @param list
     * @return
     */
    private List<JSONObject> convertToItems(List<Staff> list) {
        return list.stream().map(s -> {
            return convertToItem(s);
        }).collect(Collectors.toList());
    }

    /**
     * 对象格式转换
     * @param staff
     * @return
     */
    private JSONObject convertToItem(Staff staff) {
        JSONObject jo = new JSONObject();
        jo.put("id", staff.getId());
        jo.put("name", staff.getName());
        jo.put("sex", staff.getSex());
        jo.put("policital", staff.getPolitical());
        jo.put("department", staff.getDepartment());
        jo.put("dignity", staff.getDignity());
        jo.put("ethic", staff.getEthic());
        jo.put("birthday", staff.getBirthday());
        return jo;
    }

}
